/*
 * Copyright (C) 2014 Wei Chou (weichou2010@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tisumoon.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.view.View;
import android.view.Window;

import com.wei.c.utils.IdGetter;

/**
 * @author Wei Chou
 */
public class CostomSysUtils {
	public static final String ID_STR_STATUS_HEIGHT_VIEW		= "miui_v6_immerse_status_bar_height_view";

	public static boolean miuiV6Immerse(Activity activity, boolean blackText) {
		Window window = activity.getWindow();
		Class<?> clazz = window.getClass();
		try {
			int tranceFlag = 0;
			int darkModeFlag = 0;
			Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");

			Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_TRANSPARENT");
			tranceFlag = field.getInt(layoutParams);

			field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
			darkModeFlag = field.getInt(layoutParams);

			Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
			//只需要状态栏透明
			extraFlagField.invoke(window, tranceFlag, tranceFlag);
			//或
			if (blackText) {	//状态栏透明且黑色字体
				extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag);
			} else {	//清除黑色字体
				extraFlagField.invoke(window, 0, darkModeFlag);
			}
			return true;
		} catch (NoSuchMethodException e) {
		} catch (ClassNotFoundException e) {
		} catch (NoSuchFieldException e) {
		} catch (IllegalAccessException e) {
		} catch (IllegalArgumentException e) {
		} catch (InvocationTargetException e) {
		}
		return false;
	}

	public static void visibleStatusBarHeight(Activity activity) {
		int id = IdGetter.getId(activity, ID_STR_STATUS_HEIGHT_VIEW, IdGetter.id, false);
		if (id > 0) {
			View view = activity.findViewById(id);
			if (view != null) view.setVisibility(View.VISIBLE);
		}
	}
}
